home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / PowerPC / Dev / PPCRelease / Examples / TimerSupport / Time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-07  |  2.6 KB  |  103 lines

  1. /* 68k Timer link Module
  2.  */
  3.  
  4. #include <exec/memory.h>
  5. #include <proto/timer.h>
  6. #include <proto/exec.h>
  7. #include <stdio.h>
  8. #include "time.h"
  9.  
  10. struct TimerObject
  11. {
  12.     struct Library        *TimerBase;
  13.     struct MsgPort        *MsgPort;
  14.     struct timerequest    *IO;
  15.     struct EClockVal    MyOldEClockVal;
  16.     struct EClockVal    MyEClockVal;
  17.     ULONG            ticks;
  18. };
  19.  
  20. extern struct Library    *SysBase;
  21.  
  22. #define    TimerBase    MyTimerObject->TimerBase
  23.  
  24. void*    __regargs    TimerCreateObject(void)
  25. {
  26. struct TimerObject    *MyTimerObject;
  27.  
  28.   if (MyTimerObject=(struct TimerObject*) AllocVec(sizeof(struct TimerObject),MEMF_ANY))
  29.   {
  30.     if ((MyTimerObject->MsgPort=CreatePort(NULL, 0)) != NULL)
  31.     {
  32.       if ((MyTimerObject->IO=(struct timerequest*) CreateStdIO(MyTimerObject->MsgPort)) != NULL)
  33.       {
  34.         if (OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)MyTimerObject->IO, 0) == 0L)
  35.         {
  36.           TimerBase    =(struct Library*) MyTimerObject->IO->tr_node.io_Device;
  37.           return((void*) MyTimerObject);
  38.         }
  39.         DeleteStdIO((struct IOStdReq*) MyTimerObject->IO);
  40.       }
  41.       DeletePort(MyTimerObject->MsgPort);
  42.     }
  43.     FreeVec(MyTimerObject);
  44.   }
  45.   return(NULL);
  46. }
  47.  
  48. void    __regargs    TimerDeleteObject(struct TimerObject    *MyTimerObject)
  49. {
  50.   if (MyTimerObject)
  51.   {
  52.     CloseDevice((struct IORequest*) MyTimerObject->IO);
  53.     DeleteStdIO((struct IOStdReq*) MyTimerObject->IO);
  54.     DeletePort(MyTimerObject->MsgPort);
  55.     FreeVec(MyTimerObject);
  56.   }
  57. }
  58.  
  59. void    __regargs    TimerShow(struct TimerObject    *MyTimerObject)
  60. {
  61. ULONG            allticks,secs,mics;
  62. double            micros;
  63.  
  64.   if (MyTimerObject->MyEClockVal.ev_hi <= MyTimerObject->MyOldEClockVal.ev_hi)
  65.   {
  66.     allticks    =    MyTimerObject->MyEClockVal.ev_lo - MyTimerObject->MyOldEClockVal.ev_lo;
  67.   }
  68.   else
  69.   {
  70.     if (MyTimerObject->MyEClockVal.ev_lo <= MyTimerObject->MyOldEClockVal.ev_lo)
  71.     {
  72.       allticks    =    MyTimerObject->MyEClockVal.ev_lo - MyTimerObject->MyOldEClockVal.ev_lo;
  73.     }
  74.     else
  75.     {
  76.       allticks    =    MyTimerObject->MyOldEClockVal.ev_lo - MyTimerObject->MyEClockVal.ev_lo;
  77.     }
  78.     allticks    +=    (MyTimerObject->MyEClockVal.ev_hi - MyTimerObject->MyOldEClockVal.ev_hi)<<16;
  79.   }
  80.  
  81.   secs        =    allticks / MyTimerObject->ticks;
  82.   micros    =    ((double) 1 / ((double) MyTimerObject->ticks/ (double) ((allticks-(secs*MyTimerObject->ticks)))))*1000000;
  83.   mics        =    micros;
  84.  
  85.   printf("%ld.%06ld secs\n",secs,mics);
  86. }
  87.  
  88. BOOL    __regargs    TimerSetAttr(struct TimerObject    *MyTimerObject,
  89.                                      ULONG        Attr)
  90. {
  91.   if (Attr==TIMERTAG_START)
  92.   {
  93.     MyTimerObject->ticks        =    ReadEClock(&MyTimerObject->MyOldEClockVal);
  94.   }
  95.   else if (Attr==TIMERTAG_STOP)
  96.   {
  97.     MyTimerObject->ticks        =    ReadEClock(&MyTimerObject->MyEClockVal);
  98.   }
  99.  
  100.   return(TRUE);
  101. }
  102.  
  103.